﻿#region Using directives
using System;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProcolDemo.Geolocation;
using ProcolDemo.I18N;
using ProcolDemo.User;

#endregion

namespace ProcolDemo {
    public class Common {
        #region Gender enum
        public enum Gender {
            Male = 0,
            Female = 1
        }
        #endregion

        private static readonly I18NService I18N = new I18NService();
        private static readonly UserService userService = new UserService();

        public static void FillI18NStrings(Control page, HttpSessionState session) {
            foreach (Control c in page.Controls) {
                FillI18NStrings(c, session);

                SetControlAttributes(session, c, IsUserLoggedIn(session));
            }
        }

        private static void SetControlAttributes(HttpSessionState session, Control c, bool loggedIn) {
            if (c != null && !String.IsNullOrEmpty(c.ID)) {
                if (c.ID.StartsWith("AUTO_", StringComparison.InvariantCultureIgnoreCase)) {
                    var key = c.ID.Substring(5);

                    if (c is Label) {
                        var l = (Label) c;

                        l.Text = loggedIn
                                         ? I18N.GetTextAccordingToUserId(GetUserId(session), key)
                                         : I18N.GetText(Configuration.GetLanguage(), key);
                    } else if (c is Button) {
                        var l = (Button) c;

                        l.Text = loggedIn
                                         ? I18N.GetTextAccordingToUserId(GetUserId(session), key)
                                         : I18N.GetText(Configuration.GetLanguage(), key);
                        l.CssClass += " button ";
                    } else if (c is TextBox) {
                        var l = (TextBox) c;
                        l.CssClass = "inputbox";
                    }
                } else {
                    if (c is Button) {
                        var l = (Button) c;

                        l.CssClass += " button ";
                    } else if (c is TextBox) {
                        var l = (TextBox) c;

                        if (l.CssClass.Equals(String.Empty))
                            l.CssClass = "inputbox";
                    }
                }
            }
        }

        public static int GetUserLanguage(int uid) {
            return userService.GetUserLanguage(uid);
        }

        public static bool IsUserLoggedIn(HttpSessionState session) {
            if (session["UID"] == null || String.IsNullOrEmpty(session["UID"].ToString()))
                return false;

            return true;
        }

        public static int GetUserId(HttpSessionState session) {
            int uid;

            if (!IsUserLoggedIn(session))
                return -1;

            if (Int32.TryParse(session["UID"].ToString(), out uid))
                return uid;

            return -2;
        }

        public static int GetUserId(HttpRequest request) {
            if (request.QueryString["Uid"] == null)
                return -1;

            int uid;

            if (Int32.TryParse(request.QueryString["Uid"], out uid))
                return uid;
            // Uid is in text form, aka not numerical

            return userService.GetUserIdFromUsername(request.QueryString["Uid"]);
        }

        public static Gender GetUserGender(int uid) {
            return (Gender) userService.GetUserGender(uid);
        }
    }
}